<?
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Unit extends CI_Controller 
{
	function __construct()
	{
		parent::__construct();
		
		//load helper
		$this->load->helper('login_helper');
		
			//load model

		$this->load->model('Munit');
		


    //load libraries

    $this->load->library('pagination');

    $this->load->library('form_validation');
	// session
	 session_start();
		 if ( !isset($_SESSION['row_number']))

			$_SESSION['row_number']= 12;
	load_lang("admin");
	check_login();
	}

	function test()
	{
		$this->load->Model("Mpermission");
		$dat = $this->Mpermission->info_from_id(5);
		print_r($dat);
		$per = $dat[0]["per_module"];
		echo $per;
		$a =unserialize($per);
		load_module($a);
	}

	
	
	
	function index($page='')
	{
	
		$limit = $_SESSION['row_number'];

		$config['base_url'] = site_url().'/admin/unit/index/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Munit->count_all();

		$config['per_page'] = $limit;
		

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value

    $data['result'] = $this->Munit->list_data($page,$limit);// $this->Muser->user_review($page, $limit);

    $data['title'] = 'Danh Sách Đơn Vị Sản Phẩm';

    $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());

    $data['main'] = 'unit_review';

    $this->load->view('admin/template_main', $data);
	}
	//upddate
	function update()
	{
		$id= $this->uri->segment(4);
			if ($this->input->post('update'))

    {
	

      //set rules


        $this->form_validation->set_rules('name_vi', 'Tên  Đơn Vị(Tiếng Anh)', 'required');

      $this->form_validation->set_rules('name_en', 'Tên  Đơn Vị(Tiếng Việt)', 'required');
    

 //     $this->form_validation->set_rules('avatar', 'avatar', 'callback_upload_file');

      

      if ($this->form_validation->run() && $this->sub_update())

        redirect('admin/unit/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/unit/', 'refresh');

		}



	
		$data['main'] = 'unit_update';
$data['result']= $this->Munit->info_form_id($id);
    $data['title'] = "Cập nhật Ngành Hàng";

		$this->load->view('admin/template_main', $data);
	}
		// proccess data for update
	function sub_update()
	{
	$id= $this->uri->segment(4);
	if(isset($_POST["update"]))
		{
		
		$data1 = array(
			"en" => htmlentities($this->input->post("name_en"), ENT_QUOTES, 'UTF-8'),
			"vi" => htmlentities($this->input->post("name_vi"), ENT_QUOTES, 'UTF-8'),
	
			);
		$data = array("name" => serialize($data1));
	
			
		if($this->Munit->update($data,$id))
			return true;
		else
			return false;
		}
		else
			return false;
	}
	
	function create()
	{
	 if ($this->input->post('create'))

    {
	

      //set rules

      $this->form_validation->set_rules('name_vi', 'Tên  Đơn Vị(Tiếng Anh)', 'required');

      $this->form_validation->set_rules('name_en', 'Tên  Đơn Vị(Tiếng Việt)', 'required');


      

      if ($this->form_validation->run() && $this->sub_create())

        redirect('admin/unit/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/unit/', 'refresh');

		}



		 //reload template

		$data['main'] = 'unit_create';

    $data['title'] = 'Thêm Nghành Hàng';

		$this->load->view('admin/template_main', $data);
	}
	function check_email()
	{
		$email=$this->uri->segment(4);
		if(!$this->Muser->check_email($email))
			echo "1";
		else
			echo "0";

	}
	
	function sub_create()
	{
		if(isset($_POST["create"]))
		{
/*		if($this->Muser->check_email(!$this->input->post("email")))
		{	
			$this->form_validation->_error_array["email"] = "This Email have been existed ."; 
		
			return false;
		
		} */
		$name = array(
			"vi" => htmlentities($_POST["name_vi"],ENT_QUOTES,"UTF-8"),
			"en" => htmlentities($_POST["name_en"],ENT_QUOTES,"UTF-8"),
			);
		$data = array ( "name" => serialize($name) );
	
	
			
		if($this->Munit->insert($data))
			return true;
		else
			return false;
		}
		else
			return false;
		
		
	}
	
	
	function request()

	{

		//determine 'action'
	 

		$require = array('show', 'delete');

		foreach ($_POST as $key=>$value)

		{

			if (in_array($key, $require))

			{

				$act = $key;
				
			}

		}



		//process 'action'

		if (! empty($act))

		{

			switch ($act)

			{

				case 'show': //show

				{
				
					$_SESSION['row_number'] = $this->input->post('row_number');
					
				} break;



				case 'delete': //delete

				{

					$this->Munit->delete();

				} break;

			}//end switch



			redirect('admin/unit/', 'refresh');

		}//end if

	}
	
}









?>
